﻿using MrPP.SocketSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class RespTemperatureAndHumidity : GDGeek.Singleton<RespTemperatureAndHumidity>, Resp
    {
        public Action<Data> onRecevie
        {
            get;
            set;
        }

        public class Data
        {
            public byte area;
            public short temperature;
            public byte humidity;

            public float getTemperature()
            {
                return (float)(temperature)/10f;
            }
            public float getHumidity() {

                return (float)(humidity) ;
            }
        }
        void Start()
        {
            Debug.Log(getCommand());
            ProtoManager.Instance.addResp(this);
        }
        void OnDestroy()
        {
            if (ProtoManager.IsInitialized)
            {
                ProtoManager.Instance.removeResp(this);
            }
        }
        public void deserialize(DataStream reader)
        {
            Data data = new Data();
            data.area = reader.ReadByte();
            data.temperature = reader.ReadSInt16(true);
            Debug.Log(data.temperature);
            data.humidity = reader.ReadByte();

            Debug.Log(JsonUtility.ToJson(data));
            onRecevie?.Invoke(data);
        }

        public short getCommand()
        {
            var bytes = new byte[] { 0xFC, 0xD4 };
            return BitConverter.ToInt16(bytes, 0);
        }

        public void serialize(DataStream writer)
        {//
           // throw new System.NotImplementedException();
        }

     
    }
}